<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Settings extends MX_Controller {
    
  function __construct() {      
    
    $this->load->library('template');
    $this->load->library('form_eksternal');
    $this->load->library('global_models');
    $this->load->library('PHPExcel');
    $this->load->library('encrypt');
    $this->load->helper('path');
    $this->load->helper('url');
    $this->load->helper('form');
    $this->load->library('parser');
    $this->load->library('session');
    $this->load->library('form_validation');
    $this->load->library('pagination');
    $this->menu = $this->cek();
    
//    $this->debug($this->menu, true);
  }
	public function index(){
    $this->template->build("main", 
      array('message'     => $pesan,
            'url'         => base_url()."themes/srabon/",
            'menu_drop'   => $this->menu,
            'menu'        => "",
            'title_table' => "Settings",
            'foot'        => ""
          ));
    $this->template
      ->set_layout('default')
      ->build("main");
	} 
	public function module(){
    $module = $this->global_models->get("m_module");
    $this->template->build("module", 
      array('message'     => $pesan,
            'url'         => base_url()."themes/srabon/",
            'title_table' => "Setting Module",
            'data'        => $module,
            'foot'        => ""
          ));
    $this->template
      ->set_layout('default')
      ->build("module");
	}
	public function form(){
    $form = $this->global_models->get("m_form");
    $this->template->build("form", 
      array('message'     => $pesan,
            'url'         => base_url()."themes/srabon/",
            'title_table' => "Setting Form",
            'data'        => $form,
            'foot'        => ""
          ));
    $this->template
      ->set_layout('default')
      ->build("form");
	}
	public function control(){
    $control = $this->global_models->get_query("
      SELECT A.*, B.desc AS module
      FROM m_controller AS A
      LEFT JOIN m_module AS B ON A.id_module = B.id_module
      ");
    $this->template->build("control", 
      array('message'     => $pesan,
            'url'         => base_url()."themes/srabon/",
            'title_table' => "Setting Controller",
            'data'        => $control,
            'foot'        => ""
          ));
    $this->template
      ->set_layout('default')
      ->build("control");
	} 
	public function add_new_module($id_detail = 0){
    if(!$this->input->post(NULL)){
      $detail = $this->global_models->get("m_module", array("id_module" => $id_detail));
      $this->template->build("add-new-module", 
        array('message'     => $pesan,
              'url'         => base_url()."themes/srabon/",
              'title_table' => "Create Module",
              'detail' => $detail,
              'foot'        => ""
            ));
      $this->template
        ->set_layout('default')
        ->build("add-new-module");
    }
    else{
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "name"            => $pst['name'],
            "desc"            => $pst['desc'],
//            "status"          => $pst['status'],
            "update_by_users" => $this->session->userdata("id"),
        );
        if($this->global_models->update("m_module", array("id_module" => $pst['id_detail']),$kirim)){
          redirect("settings/module/1/Berhasil");
        }
        else{
          redirect("settings/module/2/Gagal");
        }
      }
      else{
        $kirim = array(
            "name"            => $pst['name'],
            "desc"            => $pst['desc'],
//            "status"          => $pst['status'],
            "create_by_users" => $this->session->userdata("id"),
            "create_date"     => date("Y-m-d")
        );
        if($this->global_models->insert("m_module", $kirim)){
          redirect("settings/module/1/Berhasil");
        }
        else{
          redirect("settings/module/2/Gagal");
        }
      }
    }
	}
	public function add_new_form($id_detail = 0){
    if(!$this->input->post(NULL)){
      $detail = $this->global_models->get("m_form", array("id_form" => $id_detail));
      $this->template->build("add-new-form", 
        array('message'     => $pesan,
              'url'         => base_url()."themes/srabon/",
              'title_table' => "Create Form",
              'detail' => $detail,
              'foot'        => ""
            ));
      $this->template
        ->set_layout('default')
        ->build("add-new-form");
    }
    else{
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "name"            => $pst['name'],
            "nicename"            => $pst['nicename'],
            "update_by_users" => $this->session->userdata("id"),
        );
        if($this->global_models->update("m_form", array("id_form" => $pst['id_detail']),$kirim)){
          redirect("settings/form/1/Berhasil");
        }
        else{
          redirect("settings/form/2/Gagal");
        }
      }
      else{
        $kirim = array(
            "name"            => $pst['name'],
            "nicename"            => $pst['nicename'],
            "create_by_users" => $this->session->userdata("id"),
            "create_date"     => date("Y-m-d")
        );
        if($this->global_models->insert("m_form", $kirim)){
          redirect("settings/form/1/Berhasil");
        }
        else{
          redirect("settings/form/2/Gagal");
        }
      }
    }
	}
	public function add_new_control($id_detail = 0){
    if(!$this->input->post(NULL)){
      $detail = $this->global_models->get("m_controller", array("id_controller" => $id_detail));
      $this->template->build("add-new-control", 
        array('message'     => $pesan,
              'url'         => base_url()."themes/srabon/",
              'title_table' => "Create Controller",
              'detail' => $detail,
              'module'  => $this->global_models->get_dropdown("m_module", "id_module", "desc", FALSE),
              'foot'        => ""
            ));
      $this->template
        ->set_layout('default')
        ->build("add-new-control");
    }
    else{
      $pst = $this->input->post(NULL);
      if($pst['id_detail']){
        $kirim = array(
            "name"            => $pst['name'],
            "link"            => $pst['link'],
            "id_module"       => $pst['id_module'],
            "update_by_users" => $this->session->userdata("id"),
        );
        if($this->global_models->update("m_controller", array("id_controller" => $pst['id_detail']),$kirim)){
          redirect("settings/control/1/Berhasil");
        }
        else{
          redirect("settings/control/2/Gagal");
        }
      }
      else{
        $kirim = array(
            "name"            => $pst['name'],
            "link"            => $pst['link'],
            "id_module"       => $pst['id_module'],
            "create_by_users" => $this->session->userdata("id"),
            "create_date"     => date("Y-m-d")
        );
        if($this->global_models->insert("m_controller", $kirim)){
          redirect("settings/control/1/Berhasil");
        }
        else{
          redirect("settings/control/2/Gagal");
        }
      }
    }
	}
  
  
  public function addrow_page(){
    $key = $this->input->post('no');
    $in = $this->form_eksternal->form_input("page[]");
    print <<<EOD
    <tr id='tr_{$key}'>
      <td>{$in}</td>
      <td><a href='#' class='remove-element' onclick='del_row_tambahan("tr_{$key}")' title='Remove' >Remove</a></td>
    </tr>
EOD;
    die;
  }
  public function page($id){
    $data_detail = $this->global_models->get("m_controller", array("id_controller" => $id));
    $module = $this->global_models->get("m_module", array("id_module" => $data_detail[0]->id_module));
    require_once 'application/modules/'.$module[0]->name.'/controllers/'.$data_detail[0]->link.'.php';
    $list_page = get_class_methods($data_detail[0]->link);  
//    $this->debug($list_page, true);
    foreach ($list_page as $value) {
      if($value[0] != "_"){
        if($this->global_models->get_field("m_page", "link", array("link" => $value, "id_controller" => $id)) === false){
//          die;
          $this->global_models->insert("m_page", array("id_controller" => $id, "link" => $value));
        }
      }
    }
    redirect("settings/control");
  }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */